package com.jiadakeji.ppm.modules.business.web;

import com.jiadakeji.ppm.common.utils.DateUtils;
import com.jiadakeji.ppm.common.utils.DecimalUtils;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.dao.FeeChargeDao;
import com.jiadakeji.ppm.modules.business.dao.FeeMonthDao;
import com.jiadakeji.ppm.modules.business.dao.ReportDao;
import com.jiadakeji.ppm.modules.business.entity.DTO.*;
import com.jiadakeji.ppm.modules.business.entity.FeeMonth;
import com.jiadakeji.ppm.modules.business.entity.FeeYear;
import com.jiadakeji.ppm.modules.business.service.ReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


@Controller
@RequestMapping(value = "${adminPath}/business/report")
public class ReportController extends BaseController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private FeeMonthDao feeMonthDao;
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private FeeChargeDao feeChargeDao;


    @RequiresPermissions("business:1report:view")
    @RequestMapping(value = "getReportCommunity")
    public String getReportCommunity(ReportCommunityDTO reportCommunityDTO, Model model) {
        if(StringUtils.isBlank(reportCommunityDTO.getFeeMonthId())){
            reportCommunityDTO.setFeeMonthId(feeMonthDao.getCurrentMonth().getId());
        }
       List<ReportCommunityDTO> rcd = reportService.getReportCommunity(reportCommunityDTO);
        model.addAttribute("reportCommunityDTO",reportCommunityDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportCommunityList";
    }

    @RequiresPermissions("business:1report:view")
    @RequestMapping(value = "getReportBuilding")
    public String getReportBuilding(ReportBuildingDTO reportBuildingDTO, Model model) {
        if(StringUtils.isBlank(reportBuildingDTO.getFeeMonthId())){
            reportBuildingDTO.setFeeMonthId(feeMonthDao.getCurrentMonth().getId());
        }
        List<ReportBuildingDTO> rcd = reportService.getReportBuilding(reportBuildingDTO);
        model.addAttribute("reportBuildingDTO",reportBuildingDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportBuildingList";
    }

    @RequiresPermissions("business:2report:view")
    @RequestMapping(value = "getReportPerson")
    public String getReportPerson(ReportPersonDTO reportPersonDTO, Model model) {
        if(null == reportPersonDTO.getBeginDate() && null == reportPersonDTO.getEndDate()){
            LocalDate now = LocalDate.now();
            LocalDateTime beginDate = LocalDateTime.of(now, LocalTime.MIN);
            LocalDateTime endDate = LocalDateTime.of(now,LocalTime.MAX);

            reportPersonDTO.setBeginDate(DateUtils.tranDateTimeToDate(beginDate));
            reportPersonDTO.setEndDate(DateUtils.tranDateTimeToDate(endDate));
        }
        List<ReportPersonDTO> rcd = reportDao.sumFeeReceiveByCashier(reportPersonDTO);
        Double total = 0.0;
        for (ReportPersonDTO rpd :rcd) {
            total = DecimalUtils.add(total,rpd.getTotalFee());
        }
        model.addAttribute("totalFee",total);
        model.addAttribute("reportPersonDTO",reportPersonDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportPersonList";
    }

    @RequiresPermissions("business:2report:view")
    @RequestMapping(value = "getReportPersonCharge")
    public String getReportPersonCharge(ReportPersonDTO reportPersonDTO, Model model) {
        if(null == reportPersonDTO.getBeginDate() && null == reportPersonDTO.getEndDate()){
            LocalDate now = LocalDate.now();
            LocalDateTime beginDate = LocalDateTime.of(now, LocalTime.MIN);
            LocalDateTime endDate = LocalDateTime.of(now,LocalTime.MAX);

            reportPersonDTO.setBeginDate(DateUtils.tranDateTimeToDate(beginDate));
            reportPersonDTO.setEndDate(DateUtils.tranDateTimeToDate(endDate));
        }
        List<ReportPersonDTO> rcd = reportDao.sumFeeRechargeByCashier(reportPersonDTO);
        Double total = 0.0;
        for (ReportPersonDTO rpd :rcd) {
            total = DecimalUtils.add(total,rpd.getTotalFee());
        }
        model.addAttribute("totalFee",total);
        model.addAttribute("reportPersonDTO",reportPersonDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportPersonChargeList";
    }

    @RequiresPermissions("business:3report:view")
    @RequestMapping(value = "getReportLackMoney")
    public String getReportLackMoney(ReportFeeChargeDTO reportFeeChargeDTO, Model model) {

        List<ReportFeeChargeDTO> rcd = reportDao.sumFeeChargeByCommunity(reportFeeChargeDTO);
        ReportFeeChargeDTO rf = new ReportFeeChargeDTO();
        rf.setCommunityId("0");
        rf.setCommunityName("总计");
        for (ReportFeeChargeDTO rfcd :rcd) {
            rf.setLackMoney(DecimalUtils.add(rf.getLackMoney() == null ? 0.0 : rf.getLackMoney(),rfcd.getLackMoney()));
            rf.setTotalMoney(DecimalUtils.add(rf.getTotalMoney() == null ? 0.0 : rf.getTotalMoney(),rfcd.getTotalMoney()));
        }
        rcd.add(rf);
        model.addAttribute("reportFeeChargeDTO",reportFeeChargeDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportFeeChargeList";
    }


    //房间
    @RequiresPermissions("business:3report:view")
    @RequestMapping(value = "getReportLackRoom")
    public String getReportLackRoom(@RequestParam(value = "communityId") String communityId, ReportFeeChargeRoomDTO reportFeeChargeRoomDTO, Model model) {
        ReportFeeChargeRoomDTO rfcrd = new ReportFeeChargeRoomDTO();
        rfcrd.setCommunityId(communityId);
        List<ReportFeeChargeRoomDTO> rcd = reportDao.sumFeeChargeByRoom(reportFeeChargeRoomDTO);
        model.addAttribute("reportFeeChargeRoomDTO",reportFeeChargeRoomDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportFeeChargeRoomList";
    }

    //楼栋
    @RequiresPermissions("business:3report:view")
    @RequestMapping(value = "getReportLackMoneyDetail")
    public String getReportLackMoneyDetail(@RequestParam(value = "communityId") String communityId, ReportFeeChargeBuildingDTO reportFeeChargeBuildingDTO, Model model) {
        ReportFeeChargeBuildingDTO rfcbd = new ReportFeeChargeBuildingDTO();
        rfcbd.setCommunityId(communityId);
        List<ReportFeeChargeBuildingDTO> rcd = reportDao.sumFeeChargeByBuilding(reportFeeChargeBuildingDTO);
        model.addAttribute("ReportFeeChargeBuildingDTO",reportFeeChargeBuildingDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportFeeChargeDetailList";
    }

    //楼栋
    @RequiresPermissions("business:3report:view")
    @RequestMapping(value = "getReportLackBuildingDetail")
    public String getReportLackBuildingDetail(@RequestParam(value = "communityId") String communityId, @RequestParam(value = "buildingCode") String buildingCode, ReportFeeChargeBuildingMonthDTO reportFeeChargeBuildingMonthDTO, Model model) {
        ReportFeeChargeBuildingMonthDTO rfcbmd = new ReportFeeChargeBuildingMonthDTO();
        rfcbmd.setCommunityId(communityId);
        rfcbmd.setBuildingCode(buildingCode);
        List<ReportFeeChargeBuildingMonthDTO> rcd = reportDao.sumFeeChargeByBuildingMonth(reportFeeChargeBuildingMonthDTO);
        model.addAttribute("ReportFeeChargeBuildingMonthDTO",reportFeeChargeBuildingMonthDTO);
        model.addAttribute("reportList",rcd);
        return "modules/business/reportFeeChargeBuildingDetailList";
    }



    @RequiresPermissions("business:4report:view")
    @RequestMapping(value = "feeRecycle")
    public String feeRecycle(FeeRecycleDTO feeRecycleDTO, Model model) {
        if(StringUtils.isBlank(feeRecycleDTO.getFeeMonthId())){
            feeRecycleDTO.setFeeMonthId(feeMonthDao.getCurrentMonth().getId());
        }
        FeeMonth feeMonth = feeMonthDao.get(feeRecycleDTO.getFeeMonthId());
        FeeRecycleDTO rcd = reportService.getReportFeeRecycle(feeRecycleDTO);
        rcd.setFeeMonthString(feeMonth.getMonthString());
        model.addAttribute("feeRecycleDTO",feeRecycleDTO);
        model.addAttribute("feeRecycle",rcd);
        return "modules/business/feeRecycleTable";
    }

}
